package com.jacobson.huobi.mvp.adapters

import android.content.Context
import android.text.format.DateFormat
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.jacobson.core.di.annotation.ActivityScope
import com.jacobson.core.framework.annotation.IntentParameter
import com.jacobson.core.widget.adapter.ListRecyclerAdapter
import com.jacobson.huobi.R
import com.jacobson.huobi.common.constants.AlarmTypeConstant
import com.jacobson.huobi.common.constants.LinkStatusConstant
import com.jacobson.huobi.common.constants.PeriodOffsetConstant
import com.jacobson.huobi.common.constants.PrioritySetConstant
import com.jacobson.huobi.components.CommonListRecyclerAdapter
import com.jacobson.huobi.entities.AlarmInfo
import com.jacobson.huobi.entities.SymbolRecordInfo
import com.jacobson.huobi.mvp.AlarmRecordActivity
import com.jacobson.huobi.utils.ResourcesUtil
import kotlinx.android.synthetic.main.item_symbol_alarm_record.view.*
import java.text.DecimalFormat
import javax.inject.Inject
import javax.inject.Named

@ActivityScope
class SymbolRecordAdapter @Inject constructor(@Named("context") context: Context) :
    CommonListRecyclerAdapter<SymbolRecordInfo>(context) {

    @field:Transient
    private var period: String = "60min"

    fun setPeriod(period: String) {
        this.period = period
    }

    override fun onCreateContentViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ContentViewHolder<SymbolRecordInfo> = ViewHolder(context, parent)

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        (holder as? SymbolRecordAdapter.ViewHolder)?.setPeriod(period)
        super.onBindViewHolder(holder, position)
    }

    class ViewHolder(
        context: Context?,
        parent: ViewGroup
    ) : ListRecyclerAdapter.ContentViewHolder<SymbolRecordInfo>(
        context,
        parent,
        R.layout.item_symbol_alarm_record
    ) {

        private var period: String = "60min"

        fun setPeriod(period: String) {
            this.period = period
        }

        override fun onBindView(
            items: List<SymbolRecordInfo>,
            item: SymbolRecordInfo,
            position: Int
        ) {
            with(itemView) {
//                tvSymbolName.text = "[${item.id}]${item.symbol}"
                tvSymbolName.text = item.symbol
//                tvMarketName.text = item.market.toUpperCase()
                tvMarketName.text = item.getMarketName()
                tvSequentially.text = period
                tvProfitability.text = item.profitability.toString()
                tvSequentiallyCumulative.text = item.formatAlarmProfitabilityCumulative()
                with(item) {
                    tvLinkStatusAndKLineNum.text =this.getSellMinPriorityLevel() + this.getBuyLinkStatusAndKLineNums() +this.getSellLinkStatusAndKLineNums()
                }
                when {
                    AlarmTypeConstant.isBuyType(item.buyType) -> {
                        // 启动
            //                    tvBuyType.text = alarmTypeNames[item.buyType]
                        tvBuyType.text = AlarmTypeConstant.getAlarmTypeValue(item.buyType)
                        tvRecordBuyDesc.text = AlarmTypeConstant.getRemarkByValue(item.buyType)
                        tvBuyPeriod.text = item.buyPeriod.toString()
                        tvBuyDatetime.text = DateFormat.format("MM-dd HH:mm", item.buyDataTime * 1000)
                        tvBuyPrice.text = item.buyPrice.ignoreEndZero()
                    }
                    AlarmTypeConstant.isErrorType(item.buyType) -> {
                        tvBuyType.text = "异常"
                        tvRecordBuyDesc.text = AlarmTypeConstant.getRemarkByValue(item.buyType)
                        tvBuyPeriod.text = item.buyPeriod.toString()
                        tvBuyDatetime.text = DateFormat.format("MM-dd HH:mm", item.buyDataTime * 1000)
                        tvBuyPrice.text = item.buyPrice.ignoreEndZero()
                    }
                    else -> {
                        tvBuyType.text = "-"
                        tvRecordBuyDesc.text = "-"
                        tvBuyPeriod.text = "-"
                        tvBuyDatetime.text =  "-"
                        tvBuyPrice.text = "-"
                    }
                }
                when {
                    AlarmTypeConstant.isSellType(item.sellType) -> {
                        // 启动
            //                    tvSellType.text = alarmTypeNames[item.sellType]
                        tvSellType.text = AlarmTypeConstant.getAlarmTypeValue(item.sellType)
                        tvRecordSellDesc.text = AlarmTypeConstant.getRemarkByValue(item.sellType)
                        tvSellPeriod.text = item.sellPeriod.toString()
                        tvSellDatetime.text = DateFormat.format("MM-dd HH:mm", item.sellDataTime * 1000)
                        tvSellPrice.text = item.sellPrice.ignoreEndZero()
                    }
                    AlarmTypeConstant.isErrorType(item.sellType) -> {
                        tvBuyType.text = "异常"
                        tvRecordBuyDesc.text = AlarmTypeConstant.getRemarkByValue(item.sellType)
                        tvBuyPeriod.text = item.sellPeriod.toString()
                        tvBuyDatetime.text =  DateFormat.format("MM-dd HH:mm", item.sellDataTime * 1000)
                        tvBuyPrice.text = item.sellPrice.ignoreEndZero()
                    }
                    else -> {
                        tvSellType.text = "-"
                        tvRecordSellDesc.text = "-"
                        tvSellPeriod.text = "-"
                        tvSellDatetime.text =  "-"
                        tvSellPrice.text = "-"
                    }
                }
            }

        }
    }
}
private val decimalFormat = DecimalFormat("###################.###########")
private fun Double.ignoreEndZero() = decimalFormat.format(this)

private fun SymbolRecordInfo.getSellLinkStatusAndKLineNums(): String{
    var h1Chain = PeriodOffsetConstant.comboLink4KLineChains(PeriodOffsetConstant.TYPE_MIN60, this.sellLink4KlineChain).and(0x3)
    var h4Chain = PeriodOffsetConstant.comboLink4KLineChains(PeriodOffsetConstant.TYPE_H4, this.sellLink4KlineChain).and(0x3)
    var h12Chain = PeriodOffsetConstant.comboLink4KLineChains(PeriodOffsetConstant.TYPE_H12, this.sellLink4KlineChain).and(0x3)
    var dayChain = PeriodOffsetConstant.comboLink4KLineChains(PeriodOffsetConstant.TYPE_DAY, this.sellLink4KlineChain).and(0x3)
    var h1LinkStatus = LinkStatusConstant.getLink4KLineStatus(PeriodOffsetConstant.comboLink4KLineStatus(PeriodOffsetConstant.TYPE_MIN60, this.sellLink4KlineStatus))
    var h4LinkStatus = LinkStatusConstant.getLink4KLineStatus(PeriodOffsetConstant.comboLink4KLineStatus(PeriodOffsetConstant.TYPE_H4, this.sellLink4KlineStatus))
    var h12LinkStatus = LinkStatusConstant.getLink4KLineStatus(PeriodOffsetConstant.comboLink4KLineStatus(PeriodOffsetConstant.TYPE_H12, this.sellLink4KlineStatus))
    var dayLinkStatus = LinkStatusConstant.getLink4KLineStatus(PeriodOffsetConstant.comboLink4KLineStatus(PeriodOffsetConstant.TYPE_DAY, this.sellLink4KlineStatus))
    var h1KLineNum = PeriodOffsetConstant.comboSellLink4KLineNums(PeriodOffsetConstant.TYPE_MIN60, this.sellLink4KlineNum)
    var h4KLineNum = PeriodOffsetConstant.comboSellLink4KLineNums(PeriodOffsetConstant.TYPE_H4, this.sellLink4KlineNum)
    var h12KLineNum = PeriodOffsetConstant.comboSellLink4KLineNums(PeriodOffsetConstant.TYPE_H12, this.sellLink4KlineNum)
    var dayKLineNum = PeriodOffsetConstant.comboSellLink4KLineNums(PeriodOffsetConstant.TYPE_DAY, this.sellLink4KlineNum)
    var result = ""

    if ((h1Chain == 0L || h1Chain == 1L) && h1LinkStatus == 0) {
        result += "n"
    }else {
        result += "y"
    }
    if (h1KLineNum >= 10) {
        result += ">"
    }else {
        result += h1KLineNum
    }

    if ((h4Chain == 0L || h4Chain == 1L) && h4LinkStatus == 0) {
        result += "n"
    }else {
        result += "y"
    }
    if (h4KLineNum >= 10) {
        result += ">"
    }else {
        result += h4KLineNum
    }

    if ((h12Chain == 0L || h12Chain == 1L) && h12LinkStatus == 0) {
        result += "n"
    }else {
        result += "y"
    }
    if (h12KLineNum >= 10) {
        result += ">"
    }else {
        result += h12KLineNum
    }

    if ((dayChain == 0L || dayChain == 1L) && dayLinkStatus == 0) {
        result += "n"
    }else {
        result += "y"
    }
    if (dayKLineNum >= 10) {
        result += ">"
    }else {
        result += dayKLineNum
    }

    return result

}

private fun SymbolRecordInfo.getBuyLinkStatusAndKLineNums(): String{
    var h1Chain = PeriodOffsetConstant.comboLink4KLineChains(PeriodOffsetConstant.TYPE_MIN60, this.buyLink4KlineChain).and(0x3)
    var h4Chain = PeriodOffsetConstant.comboLink4KLineChains(PeriodOffsetConstant.TYPE_H4, this.buyLink4KlineChain).and(0x3)
    var h12Chain = PeriodOffsetConstant.comboLink4KLineChains(PeriodOffsetConstant.TYPE_H12, this.buyLink4KlineChain).and(0x3)
    var dayChain = PeriodOffsetConstant.comboLink4KLineChains(PeriodOffsetConstant.TYPE_DAY, this.buyLink4KlineChain).and(0x3)
    var h1LinkStatus = LinkStatusConstant.getLink4KLineStatus(PeriodOffsetConstant.comboLink4KLineStatus(PeriodOffsetConstant.TYPE_MIN60, this.buyLink4KlineStatus))
    var h4LinkStatus = LinkStatusConstant.getLink4KLineStatus(PeriodOffsetConstant.comboLink4KLineStatus(PeriodOffsetConstant.TYPE_H4, this.buyLink4KlineStatus))
    var h12LinkStatus = LinkStatusConstant.getLink4KLineStatus(PeriodOffsetConstant.comboLink4KLineStatus(PeriodOffsetConstant.TYPE_H12, this.buyLink4KlineStatus))
    var dayLinkStatus = LinkStatusConstant.getLink4KLineStatus(PeriodOffsetConstant.comboLink4KLineStatus(PeriodOffsetConstant.TYPE_DAY, this.buyLink4KlineStatus))
    var result = ""

    if ((h1Chain == 0L || h1Chain == 1L) && h1LinkStatus == 0) {
        result += "n"
    }else {
        result += "y"
    }
    if ((h4Chain == 0L || h4Chain == 1L) && h4LinkStatus == 0) {
        result += "n"
    }else {
        result += "y"
    }

    if ((h12Chain == 0L || h12Chain == 1L) && h12LinkStatus == 0) {
        result += "n"
    }else {
        result += "y"
    }

    if ((dayChain == 0L || dayChain == 1L) && dayLinkStatus == 0) {
        result += "n"
    }else {
        result += "y"
    }
    return result + " "

}


private fun SymbolRecordInfo.getSellMinPriorityLevel(): String {
    if (this.sellPrioritySortInfo.isEmpty()) {
        return "_ "
    }
    var result = 999
    var priorityArray = this.sellPrioritySortInfo.split(",")
    var h4Level = PrioritySetConstant.getSetLevelInDiffType(priorityArray.get(0).toLong())
    var h12Level = PrioritySetConstant.getSetLevelInDiffType(priorityArray.get(1).toLong())
    var dayLevel = PrioritySetConstant.getSetLevelInDiffType(priorityArray.get(2).toLong())
    if (h4Level < result) {
        result = h4Level
    }
    if (h12Level < result) {
        result = h4Level
    }
    if (dayLevel < result) {
        result = dayLevel
    }
    if (result >= 999) {
        return "_ "
    }else {
        return result.toString() + " "
    }
}

private fun SymbolRecordInfo.getBuyLinkStatusAndKLineNum(): String {
    var linkStatus = LinkStatusConstant.getLink4KLineStatus(this.buyLink4KlineStatus)

    if (linkStatus == 0) { //当前K线实时未满足
        return "- "
    } else if (linkStatus == 1) {//当前K线实时满足
        return "★ "
    } else if (linkStatus == 2) {//当前K线实时满足(满足过)
        return "☆ "
    } else {
        return "- "
    }
}

private fun SymbolRecordInfo.getMarketName(): String {
    return when(this.market) {
        "huobi" -> "HB"
        else -> "HB"
    }
}

private fun SymbolRecordInfo.formatAlarmProfitabilityCumulative(): String {
    return ResourcesUtil.formatString(R.string.fm_symbol_double_two, this.profitabilityCumulative)
//    return if (this.profitabilityCumulative > 100) {
//            ResourcesUtil.formatString(R.string.fm_symbol_double_int, this.profitabilityCumulative)
//        }else {
//            ResourcesUtil.formatString(R.string.fm_symbol_double_one, this.profitabilityCumulative)
//        }

    }
